<script setup lang='ts'>
import { ref, reactive, provide, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import PageHeader from '@/components/PageHeader.vue'
import ShowInfoList from '@/components/ShowInfoList.vue'
import MainBodyInfo from '@/components/MainBodyInfo.vue'
import TwoItemX from '@/components/TwoItemX.vue'
import PointItem from '@/components/PointItem.vue'
import InfraredItem from '@/components/InfraredItem.vue'
import FlameCoalDialog from '@/components/FlameCoalDialog.vue'
import GasDialog from '@/components/GasDialog.vue'
import InfraredHistoryDialog from '@/components/InfraredHistoryDialog.vue'
import InfraredItemDialog from '@/components/InfraredItemDialog.vue'
import { getAlarmDataApi } from '@/api/alarm'
import { getInfraredImageApi } from '@/api/infrared'
import { mqttStart } from '@/utils/mqtt'

console.log('App')
onMounted(async () => {
  mqttStart()
  getAlarmData()
  getImage()

  let yards = document.getElementById('yard1Box')
  yardBoxWidth.value = (yards as HTMLElement).offsetWidth
  yardBoxHeight.value = (yards as HTMLElement).offsetHeight
  console.log('yards----->', yardBoxWidth.value, yardBoxHeight.value)
})

// const { startMqtt } = useMqtt()

// 控制元素显隐
const showLeftInfo = ref(false)
const showDevice = ref(true)

// 控制图标的显隐
const showIcon = reactive({
  dust: true,
  smoke: true,
  CH4: true,
  CO: true,
  O2: true,
  infrared: true
})

// refs
const flameCoalDialog = ref()
const gasDialogRefs = ref()
const infraredHistoryDialogRefs = ref()
const infraredItemDialogRefs = ref()
const showInfoListRef = ref()

// 气体数据
const gasData = reactive({
  name: '4号出口'
})

const yard1Image = reactive({
  // backgroundImage: new URL('../../assets/bckImg/des341.png', import.meta.url).href
  backgroundImage: ''
})
const yard2Image = reactive({
  backgroundImage: ''
})

// 定义气体数量
const gasType = ref(2)

const alarmData = reactive({
  CO: '999',
  O2: '999',
  SO2: '999',
  CH4: '999',
  smoke: '999',
  dust: '999',
  infrared: '999',
  flameCoal: '999',
  CO2: '99',
  O22: '99',
  SO22: '99',
  CH42: '99',
  smoke2: '99',
  dust2: '99',
  infrared2: '99',
  flameCoal2: '99'
})

// 鼠标点击的坐标
const clickX = ref(0)
const clickY = ref(0)
const yard = ref(0)
const yardBoxWidth = ref(0)
const yardBoxHeight = ref(0)

// 定义方法
const showFlameCoalDialog = (deviceId:string) => {
  // console.log('展示明火煤弹框-------->', deviceId)
  showDevice.value = false
  flameCoalDialog.value.showFlameCoal(deviceId)
}

// 获取图片
const getImage = async () => {
  let yard1 = await getInfraredImageApi({ yardId: 1 })
  console.log('获取的图片信息---->', yard1, yard1.temperFilePath.replace)
  yard1Image.backgroundImage = window.Glob.imageIp + yard1.temperFilePath.replace('wwwroot\\', '')
  let yard2 = await getInfraredImageApi({ yardId: 2 })
  yard2Image.backgroundImage = window.Glob.imageIp + yard2.temperFilePath.replace('wwwroot\\', '')
  console.log('获取的图片信息2222---->', yard2Image.backgroundImage)
}

// 展示左侧信信息
const showInfoClick = () => {
  showLeftInfo.value = !showLeftInfo.value
  showInfoListRef.value.getUnconfirmedAlarm()
  // showInfoListRef.value.getCoalYardTemperature()
}

// 关闭左侧信息栏
const closeInfoList = () => {
  showLeftInfo.value = false
}

// 展示明火煤设备
const showDeviceClick = () => {
  showDevice.value = true
}

// 控制图标显隐
const isShowIcom = (type: number) => {
  // console.log('控制图标显隐----->', type)
  switch (type) {
  case 0:
    showIcon.dust = !showIcon.dust
    break
  case 1:
    showIcon.smoke = !showIcon.smoke
    break
  case 2:
    showIcon.CH4 = !showIcon.CH4
    break
  case 3:
    showIcon.infrared = !showIcon.infrared
    break
  case 4:
    showIcon.CO = !showIcon.CO
    break
  case 5:
    showIcon.O2 = !showIcon.O2
    break
  }
}

// 获取报警阈值
const getAlarmData = async () => {
  let res = await getAlarmDataApi()
  console.log('获取报警阈值------->', res)
  if (res.code === -1) {
    ElMessage({
      // message: `${res.message}`,
      message: '网络请求中断获取数据错误！',
      type: 'warning'
    })
    return
  }
  res.forEach((item: any) => {
    switch (item.typeCode) {
    case 'CO':
      alarmData.CO = item.maxValue
      alarmData.CO2 = item.minValue
      break
    case 'CH4':
      alarmData.CH4 = item.maxValue
      alarmData.CH42 = item.minValue
      break
    case 'FOG':
      alarmData.dust = item.maxValue
      alarmData.dust2 = item.minValue
      break
    case 'SMOKE':
      alarmData.smoke = item.maxValue
      alarmData.smoke2 = item.minValue
      break
    case 'O2':
      alarmData.O2 = item.maxValue
      alarmData.O22 = item.minValue
      break
    case 'SO2':
      alarmData.SO2 = item.maxValue
      alarmData.SO22 = item.minValue
      break
    case 'Infrared':
      alarmData.infrared = item.maxValue
      alarmData.infrared2 = item.minValue
      break
    case 7:
      alarmData.flameCoal = item.maxValue
      alarmData.flameCoal2 = item.minValue
      break
    }
  })
  // console.log('获取报警阈值------->', res.result, alarmData)
}

// 鼠标移入事件
const handleMouseEnter = (e:any) => {
  console.log('鼠标移入--->', e)
}

const clickYard = (e:any, yardId:number) => {
  clickX.value = e.offsetX / yardBoxWidth.value * 400
  clickY.value = e.offsetY / yardBoxHeight.value * 125
  yard.value = yardId
  infraredHistoryDialogRefs.value.showDialog(yard.value, clickX.value, clickY.value)
  
}

const clickBottom = (e: any, id: string, yardId: number) => {
  let bottom = document.getElementById(id)
  clickX.value = e.offsetX / yardBoxWidth.value * 400
  clickY.value = (e.offsetY + (bottom as HTMLElement).offsetTop) / yardBoxHeight.value * 125
  yard.value = yardId
  infraredHistoryDialogRefs.value.showDialog(yard.value, clickX.value, clickY.value)
  
}

// 展示红外视频弹框
const showInfraredItemDialog = (name: number, id: string) => {
  console.log('展示红外视频弹框----->', name, id)
  infraredItemDialogRefs.value.showInfrarTable(name, id)
}

// 依赖注入
provide('showIcon', showIcon)
provide('isShowIcom', isShowIcom)
provide('alarmData', alarmData)
provide('getAlarmData', getAlarmData)

// provide注入的函数
const showGasDialog = (type: number, dataId: string, yard:number, code:string) => {
  console.log('展示气体弹框---->', type, dataId, yard, code)
  gasType.value = type
  gasDialogRefs.value.showGasHistoryDialog(type, yard, code)
}
provide('showGasDialog', showGasDialog)

</script>

<template>
  <div class="home-main-box">
    <div class="head-box">
      <PageHeader />
    </div>
    <div class="main-body-box">
      <div
        v-show="!showLeftInfo"
        class="left-box"
      >
        <div
          class="show-info-btn iconfont icon-zhankai"
          @click="showInfoClick"
        />
      </div>

      <div
        v-show="showLeftInfo"
        class="left-box"
      >
        <ShowInfoList ref="showInfoListRef" />
      </div>

      <div
        class="right-box"
        @click="closeInfoList"
      >
        <div class="coal-yard1">
          <img
            class="device1-box"
            :src="yard1Image.backgroundImage"
          >
          <div class="coal-yard-name">
            A煤棚
          </div>
          <div
            id="yard1Box"
            @click="clickYard($event,1)"
          >
            <div class="point-box top-box">
              <PointItem
                device-code="GS101"
                :yard="1"
              />
              <PointItem
                device-code="GS102"
                :yard="1"
              />
              <PointItem
                device-code="GS103"
                :yard="1"
              />
              <InfraredItem
                :yard="0"
                canvas-id="yard11"
                :show-infrared-item-dialog="showInfraredItemDialog"
              />
              <PointItem
                device-code="GS104"
                :yard="1"
              />
              <PointItem
                device-code="GS105"
                :yard="1"
              />
              <PointItem
                device-code="GS106"
                :yard="1"
              />
              <InfraredItem
                :yard="0"
                canvas-id="yard12"
                :show-infrared-item-dialog="showInfraredItemDialog"
              />
              <PointItem
                device-code="GS107"
                :yard="1"
              />
              <PointItem
                device-code="GS108"
                :yard="1"
              />
              <PointItem
                device-code="GS109"
                :yard="1"
              />
              <InfraredItem
                :yard="0"
                canvas-id="yard13"
                :show-infrared-item-dialog="showInfraredItemDialog"
              />
              <PointItem
                device-code="GS110"
                :yard="1"
              />
              <PointItem
                device-code="GS111"
                :yard="1"
              />
              <PointItem
                device-code="GS112"
                :yard="1"
              />
            </div>
            <div class="device1" />
            <div class="device1" />
            <div
              id="yard1Bottom"
              class="point-box bottom-box"
              @click.stop="clickBottom($event,'yard1Bottom',1)"
            >
              <div class="point">
                <PointItem
                  device-code="GS113"
                  :yard="1"
                />
                <PointItem
                  device-code="GS114"
                  :yard="1"
                />
                <PointItem
                  device-code="GS115"
                  :yard="1"
                />
              </div>
              <InfraredItem
                :yard="0"
                canvas-id="yard14"
                :show-infrared-item-dialog="showInfraredItemDialog"
              />
              <div class="point">
                <PointItem
                  device-code="GS116"
                  :yard="1"
                />
                <PointItem
                  device-code="GS117"
                  :yard="1"
                />
                <PointItem
                  device-code="GS118"
                  :yard="1"
                />
              </div>
              <InfraredItem
                :yard="0"
                canvas-id="yard15"
                :show-infrared-item-dialog="showInfraredItemDialog"
              />
              <div class="point">
                <PointItem
                  device-code="GS119"
                  :yard="1"
                />
                <PointItem
                  device-code="GS120"
                  :yard="1"
                />
                <PointItem
                  device-code="GS121"
                  :yard="1"
                />
              </div>
              <InfraredItem
                :yard="0"
                canvas-id="yard16"
                :show-infrared-item-dialog="showInfraredItemDialog"
              />
              <div class="point">
                <PointItem
                  device-code="GS122"
                  :yard="1"
                />
                <PointItem
                  device-code="GS123"
                  :yard="1"
                />
                <PointItem
                  device-code="GS124"
                  :yard="1"
                />
              </div>
            </div>
          </div>
        </div>
        <div
          class="coal-yard2"
          @click="clickYard($event, 2)"
        >
          <img
            class="device1-box"
            :src="yard2Image.backgroundImage"
          >
          <div class="coal-yard-name">
            B煤棚
          </div>
          <div class="yard-box">
            <div class="point-box top-box">
              <PointItem
                device-code="GS201"
                :yard="2"
              />
              <PointItem
                device-code="GS202"
                :yard="2"
              />
              <PointItem
                device-code="GS203"
                :yard="2"
              />
              <InfraredItem
                :yard="1"
                canvas-id="yard21"
                :show-infrared-item-dialog="showInfraredItemDialog"
              />
              <PointItem
                device-code="GS204"
                :yard="2"
              />
              <PointItem
                device-code="GS205"
                :yard="2"
              />
              <PointItem
                device-code="GS206"
                :yard="2"
              />
              <InfraredItem
                :yard="1"
                canvas-id="yard22"
                :show-infrared-item-dialog="showInfraredItemDialog"
              />
              <PointItem
                device-code="GS207"
                :yard="2"
              />
              <PointItem
                device-code="GS208"
                :yard="2"
              />
              <PointItem
                device-code="GS209"
                :yard="2"
              />
              <InfraredItem
                :yard="1"
                canvas-id="yard23"
                :show-infrared-item-dialog="showInfraredItemDialog"
              />
              <PointItem
                device-code="GS210"
                :yard="2"
              />
              <PointItem
                device-code="GS211"
                :yard="2"
              />
              <PointItem
                device-code="GS212"
                :yard="2"
              />
            </div>
            <div class="device1" />
            <!-- <div class="device1" /> -->
            <div
              id="yard2Bottom"
              class="point-box bottom-box"
              @click.stop="clickBottom($event, 'yard2Bottom',2)"
            >
              <div class="point">
                <PointItem
                  device-code="GS213"
                  :yard="2"
                />
                <PointItem
                  device-code="GS214"
                  :yard="2"
                />
                <PointItem
                  device-code="GS215"
                  :yard="2"
                />
              </div>
              <InfraredItem
                :yard="1"
                canvas-id="yard24"
                :show-infrared-item-dialog="showInfraredItemDialog"
              />
              <div class="point">
                <PointItem
                  device-code="GS216"
                  :yard="2"
                />
                <PointItem
                  device-code="GS217"
                  :yard="2"
                />
                <PointItem
                  device-code="GS218"
                  :yard="2"
                />
              </div>
              <InfraredItem
                :yard="1"
                canvas-id="yard25"
                :show-infrared-item-dialog="showInfraredItemDialog"
              />
              <div class="point">
                <PointItem
                  device-code="GS219"
                  :yard="2"
                />
                <PointItem
                  device-code="GS220"
                  :yard="2"
                />
                <PointItem
                  device-code="GS221"
                  :yard="2"
                />
              </div>
              <InfraredItem
                :yard="1"
                canvas-id="yard26"
                :show-infrared-item-dialog="showInfraredItemDialog"
              />
              <div class="point">
                <PointItem
                  device-code="GS222"
                  :yard="2"
                />
                <PointItem
                  device-code="GS223"
                  :yard="2"
                />
                <PointItem
                  device-code="GS224"
                  :yard="2"
                />
              </div>
            </div>
          </div>
        </div>
      </div>
      <GasDialog
        ref="gasDialogRefs"
        :gas-data="gasData"
        :gas-type="gasType"
      />
      <infrared-history-dialog ref="infraredHistoryDialogRefs" />
      <infrared-item-dialog ref="infraredItemDialogRefs" />
      <!-- <el-dialog
        v-model="showLeftInfo"
        :show-close="false"
        custom-class="left-info-dialog"
        top="10vh"
        width="1000px"
        :append-to-body="true"
        :modal="false"
      >
        <ShowInfoList ref="showInfoListRef" />
      </el-dialog> -->
    </div>
  </div>
</template>
<style lang="scss" scoped>
.home-main-box {
  width: 99.9%;
  height: 99vh;
  background-image: url("../../assets/bckImg/bgImg.png");
  background-repeat: no-repeat;
  background-size: 100% 100%;

  /* border: 1px solid red; */
  .head-box {
    width: 100%;
    height: 8%;

    /* border: 1px solid blue; */
  }
  .main-body-box {
    position: relative;
    display: flex;
    width: 100%;
    height: 90%;

    /* border: 1px solid red; */
    .left-box {
      position: absolute;
      z-index: 3;
      display: float;
      width: 200px;
      overflow: auto;

      /* border: 1px solid blue; */
      .show-info-btn {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 5vh;
        height: 5vh;
        margin-top: 10px;
        margin-left: 10px;
        background: #fff;

        /* border: 1px solid blue; */
        border-radius: 10px;
      }
      .show-info-btn:hover {
      cursor: pointer;
      }
    }
    .left-box::-webkit-scrollbar {
          width: 7px;
    }
    .right-box {
      position: relative;
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      justify-content: center;
      width: 100%;
      height: 100%;

      /* border: 1px solid red; */
      .coal-yard1,.coal-yard2 {
        position: relative;
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        justify-content: flex-start;
        width: 90%;
        height: 46%;
        background-image: url('../../assets/bckImg/coalYard.png');
        background-repeat: no-repeat;
        background-size: 100% 100%;
        .device1-box,
        .device2-box {
            position: absolute;
            top: 4%;
            left: 1%;
            z-index: 0;
            display: flex;
            width: 89%;
            height: 92%;
            background-size: 98% 92%;

          /* border: 1px solid red; */

            /* transform: rotate(-90deg); */
            opacity: 0.5;
          }
        .coal-yard-name {
            position: absolute;
            left: 20%;
            display: flex;
            align-items: center;
            justify-content: center;
            width: 50%;
            height: 40%;
            color: #918e8e4b;
            font-size: 130px;
            font-family: PingFang, sans-serif;
            background-image: none;

            /* border: 1px solid red; */
            transform: none;
          }

        /* border: 1px solid blue; */
          >div {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-content: space-between;
            justify-content: space-around;
            width: 90.5%;
            height: 100%;

            /* opacity: 0.2; */

            /* border: 1px solid green; */
            >div {
              width: 20%;
              height: 30%;
              background-image: url("../../assets/bckImg/device.png");
              background-repeat: no-repeat;
              background-size: 100% 100%;

              /* border: 1px solid red; */
            }
            .device1 {
              transform: rotateY(180deg);
            }
            .coal-yard-name {
              position: absolute;
              top: 35%;
              left: 25%;
              display: flex;
              justify-content: center;
              width: 50%;
              height: 30%;
              color: #918e8e4b;
              font-size: 130px;
              font-family: PingFang, sans-serif;
              background-image: none;

              /* border: 1px solid red; */
              transform: none;
            }
            .point-box {
              display: flex;
              align-items: center;
              justify-content: space-around;
              width: 100%;
              height: 33%;
              background-image: none;

              /* border: 1px solid red */
            }
            .top-box {
              align-items: flex-end;
              height: 27%;
            }
            .bottom-box {
              align-items: flex-start;
              height: 35%;

              /* border: 1px solid red; */
              .point {
                display: flex;
                align-items: flex-end;
                justify-content: space-between;
                width: 15%;
                height: 45%;

                /* border: 1px solid red */
              }
          }
          }
      }
    }
  }
}
</style>
<style lang="scss">
.el-table tr th {
  color: #272e3b;
  background-color: #1d41ff1a !important;
  border-bottom: none !important;
}
#normal {
  color: #50a1faFF;
  }
#firstStage {
background: linear-gradient(180deg, #FF7676 0%, #C62222 100%);
  }
#secondary {
background: linear-gradient(180deg, #54AAFE 0%, #4482ED 100%);
  }
#red {
  color: #EE2B00FF ;
}
#blue {
color: #005FEE;
}
#gasSecondary {
background: linear-gradient(180deg, #EE7200 0%, #FFB775 100%);
}
#gasFirstary {
background: linear-gradient(180deg, #FF7676 0%, #C62222 100%);
}
#monitorRed {
background: linear-gradient(180deg, #FF7676 0%, #C62222 100%);
box-shadow: 0 2px 4px 0 rgb(198 34 34 / 40%);
}
.gas-header-box {
  display: flex;
  .title {
    width: 120px;
    height: 28px;
    color: #4e5969;
    font-weight: 600;
    font-size: 20px;
    font-family: PingFang,sans-serif;
    line-height: 23px;
    background-clip: text;
  }
  .position-name {
    width: 252px;
    height: 28px;
    margin-left: 24px;
    color: #2191ff;
    font-weight: 600;
    font-size: 20px;
    font-family: PingFang,sans-serif;
    line-height: 23px;
    background-clip: text;
  }
}
#container {
  width: 970px;
  height: 550px;
}
.confirm-btn {
  width: 120px;
  height: 42px;
  margin-left: 420px;
  background-image: url("../../assets/bckImg/confirm.png");
}
.confirm-btn:hover {
  cursor: pointer;
}
.confirm-btn:active {
  background: rgba($color: #2191ffFF, $alpha: 90%);
  cursor: pointer;
}
.gas-dialog {
  z-index: 999;
  width: 1000px;

  /* border: 1px solid red; */
}
.el-select-dropdown__item {
  width: 150px;
}
.left-info-dialog {
  width: 15%;
  height: 90%;
  margin: 0;
  background: none;
  box-shadow: none;

  /* border: 1px solid red; */
}
</style>